using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Wootbar
{
  class Woot
  {
    /// <summary>
    /// Properties of a woot item
    /// </summary>
    public string ItemTitle { get; set; }
    public string ItemImageUrl { get; set; }
    public string ItemPrice { get; set; }
    public string ItemPurchaseUrl { get; set; }
    public string ItemCondition { get; set; }
    public int ItemQty { get; set; }
    public bool ItemSoldOut { get; set; }
    public bool IsWootOff { get; set; }
    private string RawWebSiteText { get; set; }
    //public bool   LoggedIn { get; set; }

    public Woot()
    {
      FetchWootInfo();
    }

    public string FullDescription
    {
      get
      {
        try
        {
          string rtn = string.Empty;
          if (this.IsWootOff)
          {
            if (this.ItemSoldOut)
              rtn = string.Format("Sold Out! - {0}", this.ItemTitle);
            else
              rtn = string.Format("{0} | {1} | {2}% left - {3}", this.ItemPrice, this.ItemCondition, this.ItemQty.ToString(), this.ItemTitle);
          }
          else
          {
            if (this.ItemSoldOut)
              rtn = string.Format("Sold Out! - {0}", this.ItemTitle);
            else
              rtn = string.Format("{0} | {1} - {2}", this.ItemPrice, this.ItemCondition, this.ItemTitle);
          }
          return rtn;
        }
        catch (Exception)
        {
          return this.ItemTitle;
        }
      }
    }

    private void FetchWootInfo()
    {
      this.RawWebSiteText = WebHelper.GetRawData("http://www.woot.com");
      this.ItemTitle = ParseItemTitle();
      this.ItemPrice = ParseItemPrice();
      this.ItemCondition = ParseItemCondition();
      this.ItemImageUrl = ParseImgUrl();
      this.ItemQty = ParseItemQty();
      this.ItemPurchaseUrl = ParsePurchaseUrl();
      this.ItemSoldOut = ParseItemSoldOut();
      this.IsWootOff = ParseIsWootOff();
    }

    public override string ToString()
    {
      return string.Format("{0},{1},{2},{3},{4}", ItemTitle, ItemPrice, ItemCondition, ItemImageUrl, ItemQty);
    }

    private string ParseItemTitle()
    {
      try
      {
        string strRegEx = "<h2>";
        string strEndRegEx = "</h2>";
        return StringHelper.InnerText(this.RawWebSiteText, strRegEx, strEndRegEx);
      }
      catch (Exception) { return string.Empty; }
    }

    private string ParseItemPrice()
    {
      try
      {
        string strRegEx = "<h3>";
        string strEndRegEx = "</h3>";
        return StringHelper.InnerText(this.RawWebSiteText, strRegEx, strEndRegEx);
      }
      catch (Exception) { return string.Empty; }
    }

    private string ParseItemCondition()
    {
      try
      {
        string strRegEx = "<dd>";
        string strEndRegEx = "</dd>";
        string tmp = StringHelper.InnerText(this.RawWebSiteText, strRegEx, strEndRegEx);
        int idx = tmp.IndexOf("<a href");
        if (idx > 0 )
        {
          tmp = tmp.Substring(0,idx);
        }
        return tmp;
      }
      catch (Exception) { return string.Empty; }
    }

    private bool ParseItemSoldOut()
    {
      try
      {
        string strRegEx = "class=\"soldOut\">";
        return (this.RawWebSiteText.Contains(strRegEx));
      }
      catch (Exception) { return false; }
    }

    private bool ParseIsWootOff()
    {
      try
      {
        string strRegEx = "wootOffLeft";
        return (this.RawWebSiteText.Contains(strRegEx));
      }
      catch (Exception) { return false; }
    }

    private int ParseItemQty()
    {
      try
      {
        string strRegEx = "class=\"wootOffProgressBarValue\" style=\"width:";
        string strEndRegEx = "%;";
        string ItemQty = StringHelper.InnerText(this.RawWebSiteText, strRegEx, strEndRegEx)
            .Replace(" ", "")
            .Replace("%", "");
        if (string.IsNullOrEmpty(ItemQty))
          ItemQty = "0";

        return Convert.ToInt16(ItemQty);
      }
      catch
      {
        return 0;
      }
    }

    private string ParseImgUrl()
    {
      try
      {
        string strRegEx = "class=\"mainImg\" src=\"";
        string strEndRegEx = "\" />";
        return StringHelper.InnerText(this.RawWebSiteText, strRegEx, strEndRegEx);
      }
      catch (Exception) { return string.Empty; }
    }

    private string ParsePurchaseUrl()
    {
      try
      {
        string strRegEx = "WantOne.aspx?id=";
        string strEndRegEx = "\">";
        return string.Concat("http://www.woot.com/WantOne.aspx?id=", StringHelper.InnerText(this.RawWebSiteText, strRegEx, strEndRegEx));
      }
      catch (Exception) { return string.Empty; }
    }
  }
}
